//
//  BannerCell.swift
//  HWBanner
//
//  Created by fuyoufang on 2021/10/14.
//

import Foundation
import UIKit

class BannerCell: UICollectionViewCell {
    
    var bannerSate: BannerState = .none {
        didSet {
            switch bannerSate {
            case .none:
                if titleLabel.frame.origin.x != 0 {
                    self.titleLabel.frame.origin.x = 0
                }
                
            case let .animationStage(progress):
                titleLabel.frame.origin.x = contentView.bounds.width / 2 * progress
                //debugPrint("x:\(titleLabel.frame.origin.x)")
            }
        }
    }
    
    lazy var titleLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
        label.text = "title"
        label.font = UIFont.systemFont(ofSize: 50)
        label.textColor = UIColor.black
        label.backgroundColor = .blue
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .orange
        contentView.layer.borderColor = UIColor.red.cgColor
        contentView.layer.borderWidth = 3
        setupSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupSubviews() {
        contentView.addSubview(titleLabel)
        titleLabel.frame.origin = CGPoint(x: 0,
                                          y: contentView.bounds.midY)
        
    }
    
    override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
        super.apply(layoutAttributes)
        
        if let layoutAttributes = layoutAttributes as? HWLayoutAttributes {
            bannerSate = layoutAttributes.bannerSate
        }
    }
}
